<?php

namespace App\Http\Controllers\Api;

use App\Model\PushFlow;
use App\Model\PushFlowStart;
use App\Model\PushFlowEnd;
use App\Model\UserVideoWatchRecord;
use App\Model\Video;
use App\Model\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\Api\SendMessageRequest;
use App\Model\Message;
use App\Model\MessageText;
use DB;
use Illuminate\Support\Facades\Auth;
use phpDocumentor\Reflection\Types\Null_;
use App\Model\UserViedoWatchRule;


class VideoController extends Controller
{
    public function dataList(Request $request)
    {
        $type = $request->input('type');

        $query=Video::orderBy('id','desc');
        if ($type){
            $query->where('type','=',$type);
        }

        $data = $query->paginate($request->get('limit',15))->toArray();
        $get_status = Video::get_status_all();
        foreach ($data['data'] as $key=>$val){
            $data['data'][$key]['createTime'] = date('Y-m-d H:i:s',$val['createTime']);
            //$data['data'][$key]['type'] = $get_status[$val['type']]??'';
        }

        $data = [
            'code' => 200,
            'message'   => '请求成功',
            'data'  => $data['data']
        ];
        return response()->json($data);
    }

    /**
     *  视频观看时长记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function recordVideo(Request $request)
    {
        try {
            $user = Auth::user();
            $user_id = $user->id;
            $param = $request->all();
            $video_id = $param['video_id'];
            $duration = $param['duration']??0; //观看秒数
            $frequency = $param['frequency']??0; //观看视频次数
            $record = UserVideoWatchRecord::query()
                ->where('user_id', $user_id)
                ->where('video_id', $video_id)
                ->first();
            $data = [];
            $user= Users::where('id',$user_id)->first();
            if ($record) {
                if ($duration && $record->watch_seconds <= $duration) {
                    // 如果有记录[有没有看完 && 当前时间超过已记录的时间]
                    $data['watch_seconds']  = $duration;
                }
                if ($frequency) {
                    // 如果有记录观看次数则记录次数
                    $data['frequency']  = $record->frequency+$frequency;
                }
                if($data){
                    $record->fill($data)->save();
                }
            } else {
                $data['user_id'] = $user_id;
                $data['companyId'] = $user['companyId'] ?? 0;
                $data['video_id'] = $video_id;
                $data['watch_seconds'] = $duration;
                $data['frequency']  = $frequency;
                $id = UserVideoWatchRecord::create($data);
            }
            //观看视频记录
            if($duration){
                $rule['user_id'] = $user_id;
                $rule['companyId'] = $user['companyId'] ?? 0;
                $rule['video_id'] = $video_id;
                $rule['watch_seconds'] = $duration;
                UserViedoWatchRule::create($rule);
            }

            return response()->json(['message' =>'插入成功','code' => 200], 200);
        }catch (\Exception $e) {
            return response()->json(['message' =>'插入失败','code' => 400], 200);
        }
    }

    /**
     * 用户自查自己看学习情况
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function reportlist(Request $request)
    {
        $user = Auth::user();
        $user_id = $user->id;
        $param = $request->all();
        $video_id = $param['video_id'];
        $query=UserVideoWatchRecord::orderBy('id','desc');
        $query->where('user_id','=',$user_id);
        $query->where('video_id','=',$video_id);
        $data = $query->first();;

        if($data){
            // 如果有记录观看次数则记录次数
            $dataf['frequency']  = $data->frequency+1;
            $data->fill($dataf)->save();

            $data = [
                'code' => 200,
                'message'   => '请求成功',
                'data'  => $data
            ];
        }else{
            // 如果有记录观看次数则记录次数
            $data['user_id'] = $user_id;
            $data['companyId'] = $user->companyId;
            $data['video_id'] = $video_id;
            $data['watch_seconds'] = 0;
            $data['frequency']  = 1;
            $id = UserVideoWatchRecord::create($data);
            $data = [
                'code' => 200,
                'message'   => '请求成功',
            ];
        }

        return response()->json($data);
    }

    /**
     * 获取推流地址
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function getpush(Request $request)
    {
        $data = PushFlow::orderby('id','desc')->first();
        $res_arr["data"] = $data;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 开始进入直播
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function startpush(Request $request)
    {
        $param = $request->all();
        $push_flow_id = $param['push_flow_id']; //直播推流id
        $user = Auth::user();
        $userid = $user->id;
        $data = PushFlowStart::where('push_flow_id', $push_flow_id)->where('user_id', $userid)->first();
        if(empty($data)){
            $redata['push_flow_id'] = $push_flow_id;
            $redata['user_id'] = $userid;
            $redata['created_at'] = date('Y-m-d H:i:s');
            $res = PushFlowStart::insertGetId($redata);
        }
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 退出直播
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function endpush(Request $request)
    {
        $param = $request->all();
        $push_flow_id = $param['push_flow_id']; //直播推流id
        $user = Auth::user();
        $userid = $user->id;
        $data = PushFlowEnd::where('push_flow_id', $push_flow_id)->where('user_id', $userid)->first();
        if(empty($data)){
            $redata['push_flow_id'] = $push_flow_id;
            $redata['user_id'] = $userid;
            $redata['created_at'] = date('Y-m-d H:i:s');
            $res = PushFlowEnd::insertGetId($redata);
        }
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }
}
